package process

import (
	"chat/client/utils"
	message "chat/common"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"net"
	"os"
)

type UserProcess struct {
}

// 登录
func (that *UserProcess) Login(userId int, userPwd string) {
	conn, err := net.Dial("tcp", "localhost:7788")
	if err != nil {
		fmt.Println("服务器连接错误，err=", err)
		os.Exit(1)
		return
	}
	defer conn.Close()

	var mes message.Message
	mes.Type = message.LoginMesType
	var LoginMes message.LoginMes
	LoginMes.UserId = userId
	LoginMes.UserPwd = userPwd
	data, err := json.Marshal(LoginMes)
	if err != nil {
		fmt.Println("序列化错误，err=", err)
		return
	}
	mes.Data = string(data)
	data, err = json.Marshal(mes)
	if err != nil {
		fmt.Println("序列化错误，err=", err)
		return
	}
	messagePkg := uint32(len(data))
	buffer := make([]byte, 4)
	binary.BigEndian.PutUint32(buffer, messagePkg)
	// 发送消息长度
	n, err := conn.Write(buffer[:4])
	if n != 4 || err != nil {
		fmt.Println("长度发送失败，err=", err)
		return
	}
	// 发送消息内容
	_, err = conn.Write(data)
	if err != nil {
		fmt.Println("消息发送失败，err=", err)
		return
	}
	tf := &utils.Transfer{
		Conn: conn,
	}
	mes, err = tf.ReadPkg()
	if err != nil {
		fmt.Println("消息接收失败，err=", err)
		return
	}

	var loginResMes message.LoginResMes
	err = json.Unmarshal([]byte(mes.Data), &loginResMes)
	if err != nil {
		fmt.Println("反序列化失败，err=", err)
		return
	}
	if loginResMes.Code == 200 {
		for _, v := range loginResMes.Data {
			CurUser.Conn = conn
			CurUser.UserId = userId
			CurUser.UserName = v.UserName
			CurUser.UserStatus = message.UserOnline
			if userId == v.UserId {
				continue
			}
			user := &message.User{
				UserId:     v.UserId,
				UserName:   v.UserName,
				UserStatus: message.UserOnline,
			}
			OnlineUsers[v.UserId] = *user
		}

		go processServerMes(conn)
		fmt.Println("登陆成功")
		ShowMenu()
	} else {
		fmt.Println(loginResMes.Error)
	}
}

// 注册
func (that *UserProcess) Register(userId int, userName, userPwd string) {
	conn, err := net.Dial("tcp", "localhost:7788")
	if err != nil {
		fmt.Println("服务器连接错误，err=", err)
		os.Exit(1)
		return
	}
	defer conn.Close()

	var mes message.Message
	mes.Type = message.RegisterMesType
	var RegisterMes message.RegisterMes
	RegisterMes.User.UserId = userId
	RegisterMes.User.UserName = userName
	RegisterMes.User.UserPwd = userPwd
	data, err := json.Marshal(RegisterMes)
	if err != nil {
		fmt.Println("序列化错误，err=", err)
		return
	}
	mes.Data = string(data)
	data, err = json.Marshal(mes)
	if err != nil {
		fmt.Println("序列化错误，err=", err)
		return
	}
	tf := &utils.Transfer{
		Conn: conn,
	}
	err = tf.WritePkg(data)
	if err != nil {
		fmt.Println("注册请求发送失败，err=", err)
		return
	}
	mes, err = tf.ReadPkg()
	if err != nil {
		fmt.Println("注册消息接收失败，err=", err)
		return
	}

	var registerResMes message.RegisterResMes
	err = json.Unmarshal([]byte(mes.Data), &registerResMes)
	if err != nil {
		fmt.Println("反序列化失败，err=", err)
		return
	}
	if registerResMes.Code == 200 {
		fmt.Println("注册成功")
	} else {
		fmt.Println(registerResMes.Error)
	}
	// os.Exit(0)
}
